#include "Qt3DPicker.h"

#include "Rotate2DHandler.h"
#include "CmdManager.h"
#include "Scene.h"
#include "SendPointHandlerBase.h"
#include "SelectHandler3D.h"
#include "OSGNodeMask.h"
#include "Operation/OpRotate.h"
#include "QtOSGWidget.h"

Rotate2DHandler::Rotate2DHandler()
{
}

Rotate2DHandler::~Rotate2DHandler()
{
}

void Rotate2DHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	auto scene = getScene();
	auto widget3D = getWidget();
	auto sph = getSendPointHandler();
	auto selector = qobject_cast<SelectHandler3D*>(getSelectHandler());
	if (data.type() == QVariant::Bool)
	{
		bool esc = !data.toBool();
		if (esc)
		{
			if (m_clones.size() > 0)
			{
				widget3D->pauseRenderThread();
				for (auto clone : m_clones)
				{
					m_ucs->removeChild(clone);
				}
				widget3D->resumeRenderThread();
			}
			selector->clearSelection();
			clearInput();
			emit finished();
			return;
		}
	}

	if (status() == 0)
	{
		if (selector)
		{
			selector->clearSelection();
			selector->beginSelect(D2CurveMask);
			prompt(1, tr("选择对象: "));
		}
		else
		{
			emit finished();
		}
		return;
	}

	if (status() == 1)
	{
		if (selector && selector->done())
		{
			auto selections = selector->selectionSet();
			if (selections.size() > 0)
			{
				auto selection = *selections.begin();
				std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
				auto parent = ent->getParent();
				UCS* ucs = dynamic_cast<UCS*>(parent);
				if (ucs)
				{
					m_ucs = ucs;
				}
				else
				{
					remark(tr("请选择二维图形"));
					emit finished();
					return;
				}

				widget3D->pauseRenderThread();
				for (auto selection : selections)
				{
					std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
					auto parent = ent->getParent();
					UCS* ucs = dynamic_cast<UCS*>(parent);
					if (ucs != m_ucs)
						continue;
					auto clone = ent->clone();
					m_clones.push_back(clone);
					m_origins.insert(selection);
					m_ucs->addChild(clone); 
				}
				widget3D->resumeRenderThread();

				if (sph)
				{
					sph->setPointMask(CS_LBTNDOWN | CS_MOUSEMOVE);
				}
				enableMouseHandler(MouseHandlerID::PointSender);
				cmdManager()->setPlane((Plane2DKind)m_ucs->getKind());
				prompt(2, tr("指定基点:"));
			}
		}
		return;
	}

	if(status() == 2)
	{
		osg::Vec3d ucspt;
		if (getPoint2D(*m_ucs, data, rawInput, cs, ucspt))
		{
			if (cs == CS_MOUSEMOVE)
				return;
			m_start= ucspt;
			auto matrix = m_ucs->getMatrix();
			show3DRubberLine(true);
			set3DRubberLineP1(m_start * matrix);
			set3DRubberLineP2(m_start * matrix);
			prompt(3, tr("指定旋转角:"));
		}
		return;
	}

	if (status() == 3)
	{
		if (data.type() == QVariant::Double)
		{
			double angle = data.value<double>();
			const double PI = 3.14159265358979323846;
			double rotateAngle = angle * PI / 180;
			widget3D->pauseRenderThread();
			for (auto clone : m_clones)
			{
				m_ucs->removeChild(clone);
			}
			widget3D->resumeRenderThread();
			// 记录下本次操作
			auto selections = selector->selectionSet();
			auto undoStack = scene->undoStack();
			undoStack->push(new OpRotate(m_origins, rotateAngle, osg::Vec2d(m_start.x(), m_start.y())));
			selector->clearSelection();
			emit finished();
			return;
		}
		osg::Vec3d ucspt;
		if (getPoint2D(*m_ucs, data, rawInput, cs, ucspt))
		{
			double angle = atan2(ucspt.y() - m_start.y(), ucspt.x() - m_start.x());
			//记录下本次旋转角度
			auto thisAngle = angle - m_lastAngle;
			m_lastAngle = angle;
			auto matrix = m_ucs->getMatrix();
			set3DRubberLineP2(ucspt * matrix);

			if (cs == CS_MOUSEMOVE)
			{
				for (auto clone : m_clones)
				{
					std::shared_ptr<Ent2D> ent2 = std::dynamic_pointer_cast<Ent2D>(clone);
					ent2->rotate(thisAngle, osg::Vec2d(m_start.x(),m_start.y()));
				}
			}
			else
			{
				widget3D->pauseRenderThread();
				for (auto clone : m_clones)
				{
					m_ucs->removeChild(clone);
				}
				widget3D->resumeRenderThread();
				// 记录下本次操作
				auto selections = selector->selectionSet();
				auto undoStack = scene->undoStack();
				undoStack->push(new OpRotate(m_origins, angle, osg::Vec2d(m_start.x(), m_start.y())));
				selector->clearSelection();
				emit finished();
			}
		}
		return;
	}
}